package com.ibiaoke.order.service;

import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.common.utils.DateUtils;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.order.dao.GoodsRushDao;
import com.ibiaoke.order.dao.OrderDao;
import com.ibiaoke.order.dao.OrderGoodsDao;
import com.ibiaoke.order.entity.GoodsRush;
import com.ibiaoke.order.entity.vo.GoodsRushVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class GoodsRushServiceImpl implements GoodsRushService {

    @Autowired
    private GoodsRushDao goodsRushDao;

    @Autowired
    private OrderGoodsDao orderGoodsDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 查询所有未过期抢购
     *
     * @return
     */
    @Override
    public List<GoodsRush> listRush() {
        return goodsRushDao.listRush();
    }

    /**
     * 查询进行中的抢购活动
     *
     * @param goodsId
     * @return
     */
    @Override
    public List<GoodsRush> listValidRush(Integer goodsId) {
        return goodsRushDao.listValidRush(goodsId);
    }

    /**
     * 判断数量是否合法
     *
     * @param goods
     * @param amount
     * @return
     */
    @Override
    public void validateCount(Goods goods, int amount, Integer userId, boolean reduceCount) {
        AssertUtils.notNull(goods, "商品不能为空");
        Integer goodsId = goods.getGoodsId();
        List<GoodsRush> goodsRushes = listValidRush(goodsId);
        if (CollectionUtils.isEmpty(goodsRushes)) {
          /*
            *boolean isRushGoods=CollectionUtils.isNotEmpty(listALLRush(goodsId));
            *此商品为抢购商品，抢购活动未开始或已过期"
            *AssertUtils.requireTrue(!isRushGoods,"此商品为抢购商品，不在活动期内无法购买");
            */
            return ;
        }

        if (userId != null) {
            Date begin = DateUtils.theDayBegin(0);
            Date end = DateUtils.theDayBegin(1);
            int todayBuyCount = orderDao.countOrderGoods(userId, goodsId, begin, end);
            amount += todayBuyCount;
        }

        GoodsRush goodsRush = goodsRushes.get(0);
        if (amount > goodsRush.getRemainNum()) {
            return ;
        }
        if (amount > goodsRush.getLimitNum()) {
            return ;
        }
        if (reduceCount) {
            GoodsRush updateGoodsRush = new GoodsRush();
            updateGoodsRush.setRushId(goodsRush.getRushId());
            updateGoodsRush.setRemainNum(goodsRush.getRemainNum() - amount);
            goodsRushDao.updateByPrimaryKeySelective(updateGoodsRush);
        }
        goods.setPrice(goodsRush.getRushPrice());
    }

    @Transactional
    @Override
    public void validateCount2(Goods goods, int amount, Integer userId, boolean reduceCount) {
        AssertUtils.notNull(goods, "商品不能为空");
        Integer goodsId = goods.getGoodsId();
        List<GoodsRush> goodsRushes = listValidRush(goodsId);
        if (CollectionUtils.isEmpty(goodsRushes)) {
          /*  boolean isRushGoods=CollectionUtils.isNotEmpty(listALLRush(goodsId));
            //此商品为抢购商品，抢购活动未开始或已过期"
            AssertUtils.requireTrue(!isRushGoods,"此商品为抢购商品，不在活动期内无法购买");*/
            return ;
        }

        if (userId != null) {
            Date begin = DateUtils.theDayBegin(0);
            Date end = DateUtils.theDayBegin(1);
            int todayBuyCount = orderDao.countOrderGoods(userId, goodsId, begin, end);
            amount += todayBuyCount;
        }

        GoodsRush goodsRush = goodsRushes.get(0);
        if (amount > goodsRush.getRemainNum()) {
            throw new ServiceException("剩余数量不足") ;
        }
        if (amount > goodsRush.getLimitNum()) {
            throw new ServiceException("今日抢购数量超过规定") ;
        }
        if (reduceCount) {
            GoodsRush goodsRush1 = goodsRushDao.getGoodsRushByRushIdForUpdate(goodsRush.getRushId());
            if (amount > goodsRush1.getRemainNum()) {
                throw new ServiceException("剩余数量不足") ;
            }
            GoodsRush updateGoodsRush = new GoodsRush();
            updateGoodsRush.setRushId(goodsRush.getRushId());
            updateGoodsRush.setRemainNum(goodsRush.getRemainNum() - amount);
            goodsRushDao.updateByPrimaryKeySelective(updateGoodsRush);

        }
        goods.setPrice(goodsRush.getRushPrice());
    }

    /**
     * 保存抢购活动
     *
     * @param goodsRushVO
     * @return
     */
    @Override
    public int saveGoodsRush(GoodsRushVO goodsRushVO) {
        GoodsRush goodsRush = new GoodsRush();
        BeanUtils.copyProperties(goodsRushVO, goodsRush);
        goodsRush.setCreateTime(new Date());
        goodsRush.setUpdateTime(new Date());
        goodsRush.setDelStatus(CommonConsts.NO);
        return goodsRushDao.insertSelective(goodsRush);
    }

    /**
     * 更改抢购活动信息
     *
     * @param goodsRushVO
     * @return
     */
    @Override
    public int updateGoodsRush(GoodsRushVO goodsRushVO) {
        AssertUtils.notNull(goodsRushVO.getRushId(), "抢购活动的id不能为空");
        GoodsRush goodsRush = new GoodsRush();
        BeanUtils.copyProperties(goodsRushVO, goodsRush);
        goodsRush.setUpdateTime(new Date());
        return goodsRushDao.updateByPrimaryKeySelective(goodsRush);
    }

    /**
     * 根据主键删除抢购活动
     *
     * @param rushId
     * @return
     */
    @Override
    public int deleteGoodsRush(Integer rushId) {
        AssertUtils.notNull(rushId, "抢购Id不能为空");
        GoodsRush goodsRush = new GoodsRush();
        goodsRush.setRushId(rushId);
        goodsRush.setDelStatus(CommonConsts.YES);
        goodsRush.setUpdateTime(new Date());
        return goodsRushDao.updateByPrimaryKeySelective(goodsRush);
    }

    /**
     * 查询所有的抢购活动
     *
     * @param goodsId
     * @return
     */
    @Override
    public List<GoodsRush> listALLRush(Integer goodsId) {
        return goodsRushDao.listAllRush(goodsId);
    }
}
